/*
I needed to overload the date object to get some functionality from it
*/
Date.prototype.subtractDate = function( subtrahend ) {
	difference = new Date();
	difference.setTime(Math.abs(this.getTime() - subtrahend.getTime()));
	return difference;	
}

Date.prototype.addDate = function( addend ) {
	summand  = new Date();
	summand.setTime(Math.abs(this.getTime() - addends.getTime()));
	return summand;
}
//Date.prototype._monthnames = Array("January","February","March","April","May", "June","July","August","September","October","November","December");
Date.prototype.addByPartName = function(strPart, addend )
{
	switch(strPart)
	{
		case 'day':
			this.setDate(this.getDate() + addend);
			break;
		case 'month':
			this.setMonth(this.getMonth() + addend);
			break;
		case 'year':
			this.setFullYear(this.getFullYear() + addend);
			break;
		case 'hour':
			this.setHours(this.getHours() + addend);
			break;
		case 'minute':
			this.setMinutes(this.getMinutes() + addend);
			break;
		case 'second':
			this.setSeconds(this.getSeconds() + addend);
			break;
		case 'millisecond':
			this.setMilliseconds(this.getMilliseconds() + addend);
			break;
		default:
			this.setTime(this.getTime() + addend);
	}
}

Date.prototype.getByPartName = function(strPart, bePretty)
{
	switch(strPart)
	{
		case 'day':
			if (bePretty)
			{
				mePretty =  (100 + this.getDate()) + ' ';
				return mePretty.substr(1,2);
			}
			else
			{ 
				return this.getDate();
			}
			break;
		case 'weekday':
			return this.getDay();
			break;
		case 'month':
			return bePretty ? this.getMonth() + 1 : this.getMonth();
			break;
		case 'year':
			return this.getFullYear(); // this.getFullYear().toString().substr(2,2)
			break;
		case 'decade':
			return Math.floor(this.getFullYear() / 10) + '0s';
			break;
		case 'century':
			return Math.floor(this.getFullYear() / 100) + '00';
			break;
		case 'millenium':
			return Math.floor(this.getFullYear() / 1000) + '000';
			break;
		case 'hour':
			return this.getHours();
			break;
		case 'minute':
			return this.getMinutes();
			break;
		case 'second':
			return this.getSeconds();
			break;
		case 'millisecond':
			return this.getMilliseconds();
			break;
		default:
			return this.getTime();
	}
};

Date.prototype.setByPartName = function(strPart, value)
{
	switch(strPart)
	{
		case 'day':
			this.setDate(value);
			break;
		case 'month':
			this.setMonth(value);
			break;
		case 'year':
			this.setFullYear(value);
			break;
/*
		case 'decade':
			this.getFullYear() / 10;
			break;
		case 'century':
			this.getFullYear() / 100;
			break;
		case 'millenium':
			this.getFullYear() / 1000;
			break;
*/
		case 'hour':
			this.setHours(value);
			break;
		case 'minute':
			this.setMinutes(value);
			break;
		case 'second':
			this.setSeconds(value);
			break;
		case 'millisecond':
			this.setMilliseconds(value);
			break;
		default:
			this.setTime(value);
	}
};

// if bTotal, returns the total parts count since 1970
// else returns the count within the current dates context 
Date.prototype.getByPartNameCount = function(strPart)
{
	num = this.getTime();
	switch(strPart)
	{
		case 's':
			return Math.floor(num / (1000));
			break;
		case 'min':
			return Math.floor(num / (1000 * 60));
			break;
		case 'h':
			return Math.floor(num / (1000 * 60 * 60));
			break;
		case 'day':
			return Math.floor(num / (1000 * 60 * 60 * 24));
			break;
		case 'week':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 7));
			break;
		case 'month':
			// 30.4375 = average month length (365.25/12)... is this a bs way to do it?  I'll keep my eye out for a better way
			return Math.floor(num / (1000 * 60 * 60 * 24 * 30.4375)); 
			break;
		case 'year':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25));
			break;
		case 'decade':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25 * 10));
			break;
		case 'century':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25 * 100));
			break;
		case 'millenium':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25 * 1000));
			break;
		default:
			return this.getTime();
	}
};
function LZ(x){return(x<0||x>9?"":"0")+x};
// a global month names array
var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
/*
Field        | Full Form          | Short Form
-------------|--------------------|-----------------------
Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
             | NNN (abbr.)        |
Day of Month | dd (2 digits)      | d (1 or 2 digits)
Day of Week  | EE (name)          | E (abbr)
Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)
Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)
Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)
Minute       | mm (2 digits)      | m (1 or 2 digits)
Second       | ss (2 digits)      | s (1 or 2 digits)
AM/PM        | a                  |

*/
Date.prototype.format = function(format){format=format+"";var result="";var i_format=0;var c="";var token="";var y=this.getYear()+"";var M=this.getMonth()+1;var d=this.getDate();var E=this.getDay();var H=this.getHours();var m=this.getMinutes();var s=this.getSeconds();var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;var value=new Object();if(y.length < 4){y=""+(y-0+1900);}value["y"]=""+y;value["yyyy"]=y;value["yy"]=y.substring(2,4);value["M"]=M;value["MM"]=LZ(M);value["MMM"]=MONTH_NAMES[M-1];value["NNN"]=MONTH_NAMES[M+11];value["d"]=d;value["dd"]=LZ(d);value["E"]=DAY_NAMES[E+7];value["EE"]=DAY_NAMES[E];value["H"]=H;value["HH"]=LZ(H);if(H==0){value["h"]=12;}else if(H>12){value["h"]=H-12;}else{value["h"]=H;}value["hh"]=LZ(value["h"]);if(H>11){value["K"]=H-12;}else{value["K"]=H;}value["k"]=H+1;value["KK"]=LZ(value["K"]);value["kk"]=LZ(value["k"]);if(H > 11){value["a"]="PM";}else{value["a"]="AM";}value["m"]=m;value["mm"]=LZ(m);value["s"]=s;value["ss"]=LZ(s);while(i_format < format.length){c=format.charAt(i_format);token="";while((format.charAt(i_format)==c) &&(i_format < format.length)){token += format.charAt(i_format++);}if(value[token] != null){result=result + value[token];}else{result=result + token;}}return result;};

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function allparts()
{

	timediff = diff.getTime();

	weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
	timediff -= weeks * (1000 * 60 * 60 * 24 * 7);
	
	days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
	timediff -= days * (1000 * 60 * 60 * 24);
	
	hours = Math.floor(timediff / (1000 * 60 * 60)); 
	timediff -= hours * (1000 * 60 * 60);
	
	mins = Math.floor(timediff / (1000 * 60)); 
	timediff -= mins * (1000 * 60);
	
	secs = Math.floor(timediff / 1000); 
	timediff -= secs * 1000;
	
	dateform.difference.value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";	
}